//
//    Copyright (C) 2012  OinkStudio
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
package game;

import java.io.Serializable;

public abstract class Rule implements Serializable {

	private String ruleName;
	private int priority;
	// mostrar si s'ha activat la norma (utilitzat per capturar el nom de la norma)
	//private boolean activated = false;

	public Rule(String ruleName, int priority) {
		this.priority = priority;
		this.ruleName = ruleName;
	}

	/**
	 * @return Returns the ruleName.
	 * @uml.property name="ruleName" readOnly="true"
	 */
	public String getRuleName() {
		return ruleName;

	}

	/**
	 */
	public abstract void check(int row, int column, Game game);

	protected int[] checkNorth(int row, int column, Game game) {
		/*
		 * int[] vecCompare = new int[2]; vecCompare[0] =
		 * board[row][column].getNorth(); vecCompare[1] =
		 * board[row-1][column].getSouth(); return vecCompare;
		 */
		Obolo[][] board = game.getBoard();
		try {
			if (board[row - 1][column].getOwner() != board[row][column]
					.getOwner()) {
				Skill primer = board[row][column].getNorth();
				Skill segon = board[row - 1][column].getSouth();
				int[] toReturn;
				if (primer instanceof SkillNumber
						&& segon instanceof SkillNumber) {
					toReturn = new int[2];
					toReturn[0] = ((SkillNumber) primer).getNumber();
					toReturn[1] = ((SkillNumber) segon).getNumber();
					return toReturn;
				} else {
					primer.check(segon, game, row - 1, column);
					return null;
				}
			} else
				return null;
		} catch (Exception e) {
			return null;
		}
	}

	protected int[] checkEast(int row, int column, Game game) {
		/*
		 * int[] vecCompare = new int[2]; vecCompare[0] =
		 * board[row][column].getEast(); vecCompare[1] =
		 * board[row][column+1].getWest(); return vecCompare;
		 */
		Obolo[][] board = game.getBoard();
		try {
			if (board[row][column + 1].getOwner() != board[row][column]
					.getOwner()) {
				Skill primer = board[row][column].getEast();
				Skill segon = board[row][column + 1].getWest();
				int[] toReturn;
				if (primer instanceof SkillNumber
						&& segon instanceof SkillNumber) {
					toReturn = new int[2];
					toReturn[0] = ((SkillNumber) primer).getNumber();
					toReturn[1] = ((SkillNumber) segon).getNumber();
					return toReturn;
				} else {
					primer.check(segon, game, row, column + 1);
					return null;
				}
			} else
				return null;
		} catch (Exception e) {
			return null;
		}

	}

	protected int[] checkSouth(int row, int column, Game game) {
		Obolo[][] board = game.getBoard();
		try {
			if (board[row + 1][column].getOwner() != board[row][column]
					.getOwner()) {
				Skill primer = board[row][column].getSouth();
				Skill segon = board[row + 1][column].getNorth();
				int[] toReturn;
				if (primer instanceof SkillNumber
						&& segon instanceof SkillNumber) {
					toReturn = new int[2];
					toReturn[0] = ((SkillNumber) primer).getNumber();
					toReturn[1] = ((SkillNumber) segon).getNumber();
					return toReturn;
				} else {
					primer.check(segon, game, row + 1, column);
					return null;
				}
			} else
				return null;
		} catch (Exception e) {
			return null;
		}
	}

	protected int[] checkWest(int row, int column, Game game) {
		Obolo[][] board = game.getBoard();
		try {
			if (board[row][column - 1].getOwner() != board[row][column]
					.getOwner()) {
				Skill primer = board[row][column].getWest();
				Skill segon = board[row][column - 1].getEast();
				int[] toReturn;
				if (primer instanceof SkillNumber
						&& segon instanceof SkillNumber) {
					toReturn = new int[2];
					toReturn[0] = ((SkillNumber) primer).getNumber();
					toReturn[1] = ((SkillNumber) segon).getNumber();
					return toReturn;
				} else {
					primer.check(segon, game, row, column - 1);
					return null;
				}
			} else {
				return null;
			}
		} catch (Exception e) {
			return null;
		}

	}

	public int getPriority() {
		return priority;
	}

}
